﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

namespace template_dll
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        public SqlConnection conn;
        private string cell;

        private void Form4_Load(object sender, EventArgs e)
        {
            CategSell();
        }

        private void CategSell()
        {
            try
            {
                DataTable[] dt_res = new SqlStorProc().SqlProcExecWithQuery(conn, "pas_Actions_PharmCategSel", 300, new SqlStorProc.sp_params[0]);
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = dt_res[0].DefaultView;
                dt_res[0].Columns[2].ReadOnly = false;
                dataGridView1.ReadOnly = false;
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i != 2) dataGridView1.Columns[i].ReadOnly = true;
                }
                label1.Text = Convert.ToDateTime(dt_res[1].Rows[0][0]).ToString("dd.MM.yyyy");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[2];
                par[0].name = "@date1";
                par[1].name = "@date2";
                par[0].value = dateTimePicker1.Value.Date;
                par[1].value = dateTimePicker2.Value.Date;
                dataGridView1.DataSource = null;
                DataTable dt_res = new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_PharmCategNew", 300, par);
                dt_res.Columns[2].ReadOnly = false;
                dataGridView1.DataSource = dt_res.DefaultView;
                dataGridView1.ReadOnly = false;
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i != 2) dataGridView1.Columns[i].ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
                par[0].name = "@pharmcat";
                DataTable ins = new DataTable();
                ins.Columns.Add("PharmCateg");
                ins.Columns.Add("PharmCode");
                ins.Columns.Add("SumRet");
                if (dataGridView1.RowCount > 0)
                {
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        object[] obj = new object[3] { row.Cells["Категория"].Value, row.Cells["Код аптеки"].Value, row.Cells["Сумма"].Value };
                        ins.Rows.Add(obj);
                    }
                }
                par[0].value = ins;
                new SqlStorProc().SqlProcExecNonQuery(conn, "pas_Actions_PharmCategIns", 300, par);
                MessageBox.Show("Все данные сохранены");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Закрыть окно без сохранения?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) 
                this.DialogResult = DialogResult.Cancel;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() != cell)
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Coral;
        }

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            cell = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                new ExpToExcel().ExportToExcel((dataGridView1.DataSource as DataView).ToTable());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
